<?php
/**
 * @package		简易CMS
 * @subpackage	缓存：插件
 * @version		$Id: function_delete.php 25 2012-08-29 17:55:19Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if(!defined('IN_JYCMS')) {
	exit('Access Denied');
}

/**
 * 删除会员
 *
 * @param array $uids 要删除的会员ID列表
 * @param unknown_type $other
 * @return unknown
 */
function deletemember($uids) {
	$numdeleted = DB::result_first("SELECT count(*) FROM #__common_member WHERE uid IN ($uids)");

	DB::query("DELETE FROM #__common_credit_rule_log WHERE uid IN ($uids)", 'UNBUFFERED');

	DB::query("DELETE FROM #__common_notification WHERE (uid IN ($uids) OR authorid IN ($uids))", 'UNBUFFERED');

	DB::query("DELETE FROM #__common_clickuser WHERE uid IN ($uids)", 'UNBUFFERED');

	DB::query("DELETE FROM #__common_member WHERE uid IN ($uids)", 'UNBUFFERED');
	return $numdeleted;
}

/**
 * 删除回收站中的项目
 *
 * @param array $aids 要删除的记录ID列表
 * @return int 删除的记录数
 */
function deletetrasharticle($aids) {
	global $_G;

	$articles = $trashid = $pushs = $dels = array();
	$query = DB::query("SELECT * FROM #__common_trash WHERE aid IN (".dimplode($aids).")");
	while ($value = DB::fetch($query)) {
		$dels[$value['aid']] = $value['aid'];
		$article = unserialize($value['content']);
		$articles[$article['aid']] = $article;
		if(!empty($article['idtype'])) $pushs[$article['idtype']][] = $article['id'];
		if($article['pic']) {
			@unlink($_G['config']['attachdir'].'./'.$article['pic']);
		}
	}

	if($dels) {
		DB::query("DELETE FROM #__common_trash WHERE aid IN(".dimplode($dels).")", 'UNBUFFERED');
		deletearticlerelated($dels);
	}

	return $articles;
}

/**
 * 删除文章
 *
 * @param array $aids 要删除的文章ID列表
 * @param boolean $istrash 是否先放入回收站
 * @return array 已经删除的文章
 */
function deletearticle($aids, $istrash = 1) {
	global $_G;

	if(empty($aids)) return false;
	$trasharr = $article = $bids = $dels = $attachment = $attachaid = $catids = $pushs = array();
	$query = DB::query("SELECT * FROM #__portal_article_title WHERE aid IN (".dimplode($aids).")");
	while ($value = DB::fetch($query)) {
		$catids[] = intval($value['catid']);
		$dels[$value['aid']] = $value['aid'];
		$article[] = $value;
		if(!empty($value['idtype'])) $pushs[$value['idtype']][] = $value['id'];
	}
	if($dels) {
		foreach((array)$article as $key => $value) {
			if($istrash) {
				$valstr = daddslashes(serialize($value));
				$trasharr[] = "('$value[aid]', '$valstr')";
			} elseif($value['pic']) {
				pic_delete($value['pic'], 'portal', $value['thumb'], $value['remote']);
				$attachaid[] = $value['aid'];
			}
		}
		if($istrash) {
			if($trasharr) {
				DB::query("INSERT INTO #__common_trash (`aid`, `content`) VALUES ".implode(',', $trasharr));
			}
		} else {
			deletearticlerelated($dels);
		}

		DB::query("DELETE FROM #__portal_article_title WHERE aid IN(".dimplode($dels).")", 'UNBUFFERED');

		$catids = array_unique($catids);
		if($catids) {
			foreach((array)$catids as $catid) {
				$cnt = DB::result_first("SELECT COUNT(*) FROM #__portal_article_title WHERE catid = '$catid'");
				DB::update('common_category', array('articles'=>$cnt), array('catid'=>$catid));
			}
		}
	}
	return $article;
}

/**
 * 删除文章连同相关的项目
 *
 * @param array $dels 要删除的文章ID列表
 */
function deletearticlerelated($dels) {

	DB::query("DELETE FROM #__portal_article_content WHERE aid IN(".dimplode($dels).")", 'UNBUFFERED');

	$query = DB::query("SELECT * FROM #__common_attachment WHERE aid IN (".dimplode($dels).") AND aidtype='article'");
	while ($value = DB::fetch($query)) {
		$attachment[] = $value;
		$attachdel[] = $value['attachid'];
	}
	foreach ((array)$attachment as $value) {
		pic_delete($value['attachment'], 'portal', $value['thumb'], $value['remote']);
	}
	DB::query("DELETE FROM #__common_attachment WHERE aid IN (".dimplode($dels).") AND aidtype='article'", 'UNBUFFERED');

	DB::query("DELETE FROM #__common_comment WHERE aid IN(".dimplode($dels).")", 'UNBUFFERED');

	DB::query("DELETE FROM #__portal_article_related WHERE aid IN(".dimplode($dels).")", 'UNBUFFERED');

}
